﻿using Data;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Docking;
using DevExpress.XtraBars.Docking2010.Views.Tabbed;
using DevExpress.XtraEditors;
using Event;
using Git;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace GitFrom
{
    class FromBranch
    {
        private static BarSubItem bsis;
        private static DockManager dockManager;
        
        /// <summary>
        /// 点击 分支 执行切换分支
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void p_ItemClick_OutputDataReceived(object sender, ItemClickEventArgs e)
        {

            //切换分支
            gitconfig gc = git.gitexelist.Find(x => x.Value == "GITProductURL");
            if (gc != null)
            {
                DialogResult dr = XtraMessageBox.Show("切换分支将关闭所有标签", "确定要关闭吗？", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (dr == DialogResult.OK)
                {
                    if (bsis != null)
                    {
                        List<DockPanel> ldp = new List<DockPanel>();
                        foreach (DockPanel dp in dockManager.Panels)
                        {
                            if (!Regex.IsMatch(dp.Name, @"dockPanel_目录"))
                            {
                                ldp.Add(dp);
                            }
                        }
                        foreach (DockPanel dp in ldp)
                        {
                            dockManager.RemovePanel(dp);
                        }

                        if (gc.Key != null && gc.Key.Length > 0)
                        {
                            if (e.Item.Caption.Equals("local/本地分支"))
                            {
                                GlobalDirectory.SaveDirectory = "JSONData";
                                EventGlobal.SetRequestDirectoryReloadList();
                                bsis.Caption = e.Item.Caption;
                                GlobalDirectory.GitRemote = false;
                                return;
                            }
                            else 
                            {
                                git g = new git();
                                //if (g.gitpush(gc.Key)) { 
                                    if (g.gitcheckout(gc.Key, e.Item.Caption.Replace("origin/", "").Trim())) {
                                        GlobalDirectory.SaveDirectory = "JSONGitDataList\\" + gc.Branch.ToString();
                                        EventGlobal.SetRequestDirectoryReloadList();
                                        bsis.Caption = e.Item.Caption;
                                        GlobalDirectory.GitRemote = true;
                                        return;
                                    };
                                //};
                                
                            }
                            XtraMessageBox.Show("分支切换失败", "GIT分支切换失败！", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);

                        }
                        Console.WriteLine(e.Item.Caption);
                    }
                }
            }
            else
            {
                XtraMessageBox.Show("请先配置GIT", "GIT未配置？", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            }       
        }

        /// <summary>
        /// 设置 主页 上的git分支信息
        /// </summary>
        /// <param name="bsi"></param>
        /// <param name="_dockManager"></param>
        public void branch(BarSubItem bsi = null, DockManager _dockManager = null)
        {
            if (bsi != null)
            {
                git.LoadToTree(null);
                bsis = bsi;
                dockManager = _dockManager;
            }
            bsis.ClearLinks();
            BarButtonItem bi2 = new BarButtonItem();
            bi2.Caption = "local/本地分支";
            bi2.ItemClick += p_ItemClick_OutputDataReceived;
            bsis.AddItem(bi2);
            //如果有git配置，初始化设置分支为本地分支
            if (Git.git.fetchcodelist.Count > 0)
            {
                bsis.Caption = "local/本地分支";
            }
            foreach (gitconfig gcf in Git.git.fetchcodelist)
            {
                BarButtonItem bi = new BarButtonItem();
                bi.Caption = gcf.Key;
                bi.ItemClick += p_ItemClick_OutputDataReceived;
                bsis.AddItem(bi);
            }
        }

    }
}
